package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Score;
import com.example.service.ScoreService;
import com.example.vo.ScoreQueryVO;
import com.example.vo.ScoreResultVO;
import com.example.common.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "学生成绩管理", description = "学生成绩的增删改查与多维度查询")
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;


    @Operation(summary = "新增学生成绩", description = "新增一条学生成绩记录")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "新增成功"),
        @ApiResponse(responseCode = "-1", description = "新增失败")
    })
    @PostMapping
    public ApiResult<Boolean> addScore(@RequestBody @Parameter(description = "成绩实体", required = true) Score score) {
        boolean success = scoreService.saveScore(score);
        return success ? ApiResult.success("新增成功", true) : ApiResult.error("新增失败");
    }


    @Operation(summary = "修改学生成绩", description = "根据ID修改学生成绩")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "修改成功"),
        @ApiResponse(responseCode = "-1", description = "修改失败")
    })
    @PutMapping
    public ApiResult<Boolean> updateScore(@RequestBody @Parameter(description = "成绩实体", required = true) Score score) {
        boolean success = scoreService.updateScore(score);
        return success ? ApiResult.success("修改成功", true) : ApiResult.error("修改失败");
    }


    @Operation(summary = "删除学生成绩", description = "根据ID删除学生成绩")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "删除成功"),
        @ApiResponse(responseCode = "-1", description = "删除失败")
    })
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> deleteScore(@PathVariable @Parameter(description = "成绩ID", required = true) Long id) {
        boolean success = scoreService.deleteScore(id);
        return success ? ApiResult.success("删除成功", true) : ApiResult.error("删除失败");
    }


    @Operation(summary = "查询单条学生成绩", description = "根据ID查询学生成绩")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "查询成功"),
        @ApiResponse(responseCode = "-1", description = "未找到成绩")
    })
    @GetMapping("/{id}")
    public ApiResult<Score> getScore(@PathVariable @Parameter(description = "成绩ID", required = true) Long id) {
        Score score = scoreService.getScoreById(id);
        if (score == null) {
            return ApiResult.notFound("未找到成绩");
        }
        return ApiResult.success("查询成功", score);
    }


    @Operation(summary = "分页多条件查询学生成绩", description = "支持年级、班级、学期、课程、学生姓名模糊等多条件分页查询")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "查询成功")
    })
    @PostMapping("/query")
    public ApiResult<IPage<ScoreResultVO>> queryScores(
            @RequestBody @Parameter(description = "查询条件", required = true) ScoreQueryVO queryVO,
            @RequestParam(defaultValue = "1") @Parameter(description = "页码") int pageNum,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量") int pageSize) {
        Page<ScoreResultVO> page = new Page<>(pageNum, pageSize);
        IPage<ScoreResultVO> result = scoreService.queryScores(page, queryVO);
        return ApiResult.success("查询成功", result);
    }
}
